package com.example.androidapp2;

import java.util.List;

import com.example.androidapp2.DbClasses.TotalCategoria;
import com.example.androidapp2.DbClasses.Utils;
import com.xyzdesa.droidnomy.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

public class PopUpPorcentajes extends Dialog {	
	DbAdapter DbConect;
	Context Ctx;
	GridView grdPorcentajes;
	ImageButton btnCerrar;
	Spinner spnCateFecha;
	TextView lblTotalesIn;
	TextView lblTotalesEg;
	TextView txtAhorro;	
	Activity Act;	

	public PopUpPorcentajes(Context context, DbAdapter db,Activity act) {
		super(context);
		Ctx = context;
		DbConect = db;
		Act = act;	

		this.setTitle(R.string.pupPorcen);
		this.setContentView(R.layout.pantalla_porcentajes);

		grdPorcentajes = (GridView) findViewById(R.id.grdPorcentajes);
		btnCerrar = (ImageButton) findViewById(R.id.btnCerrar);
		spnCateFecha = (Spinner) findViewById(R.id.spnCateFecha);
		lblTotalesIn = (TextView) findViewById(R.id.lblTotalesIn);
		lblTotalesEg = (TextView) findViewById(R.id.lblTotalesEg);
		txtAhorro = (TextView) findViewById(R.id.txtAhorro);		
	}

	public void CargarPantallaPorcentajes()
	{
		int In = DbConect.GetTotalMovimientos("In");
		int Eg = DbConect.GetTotalMovimientos("Eg");

		//lblTotalesIn.setText("$"+In);
		//lblTotalesEg.setText("$"+Eg);

		txtAhorro.setText(Ctx.getResources().getString(R.string.txtAhorro) + " $" + String.valueOf(In-Eg));		
		Utils.SetControlColor(txtAhorro, (In-Eg)>0);
		Utils.SetControlColor(lblTotalesIn, true);
		Utils.SetControlColor(lblTotalesEg, false);

		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(Ctx,android.R.layout.simple_spinner_item, DbConect.GetFechasSpinner());
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);		
		spnCateFecha.setAdapter(spinnerAdapter);	

		spnCateFecha.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {				 
				Resources res = Ctx.getResources();				
				if(arg0.getSelectedItem().toString().equals(res.getString(R.string.spnItemCate))){					
					lblTotalesIn.setText(res.getString(R.string.pupIngresos) + " $"+DbConect.GetTotalMovimientos("In"));
					lblTotalesEg.setText(res.getString(R.string.pupEgresos) + " $"+DbConect.GetTotalMovimientos("Eg"));
					CargarGrilla(DbConect.GetTotalesPorCategoria());
				}
				else if(arg0.getSelectedItem().toString().length()==4){
					lblTotalesIn.setText(res.getString(R.string.pupIngresos) + " $"+DbConect.GetTotalMovimientosAnio("In", arg0.getSelectedItem().toString()));
					lblTotalesEg.setText(res.getString(R.string.pupEgresos) + " $"+DbConect.GetTotalMovimientosAnio("Eg",arg0.getSelectedItem().toString()));
					CargarGrilla(DbConect.GetTotalesPorCategoriaAnio(arg0.getSelectedItem().toString()));
				}
				else{
					lblTotalesIn.setText(res.getString(R.string.pupIngresos) + " $"+DbConect.GetTotalMovimientosMes("In", arg0.getSelectedItem().toString().replace(" - ", "")));
					lblTotalesEg.setText(res.getString(R.string.pupEgresos) + " $"+DbConect.GetTotalMovimientosMes("Eg",arg0.getSelectedItem().toString().replace(" - ", "")));
					CargarGrilla(DbConect.GetTotalesPorCategoriaMes(arg0.getSelectedItem().toString().replace(" - ", "")));
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {}

		});

		CargarGrilla(DbConect.GetTotalesPorCategoria());

		final Dialog di = this;		
		btnCerrar.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				di.hide();				
			}
		});
	}

	public void CargarGrilla(final List<TotalCategoria> list)
	{		
		if(list.size()>0){
			ArrayAdapter<TotalCategoria> gridAdapter = new ArrayAdapter<TotalCategoria>(Ctx,android.R.layout.simple_list_item_2, list){
				@Override			
				public View getView(int position, View convertView, ViewGroup parent)
				{
					int total =DbConect.GetTotalMovimientos("Eg");
					TotalCategoria tt = list.get(position);
					int porcentaje = (total>0)?(tt.Total*100)/total:0;				
					LayoutInflater li = getLayoutInflater();

					View MyView = li.inflate(R.layout.grd_porcentajes_item, null);
					TextView tv = (TextView)MyView.findViewById(R.id.grd_item_txtDesc);
					tv.setText(tt.Descripcion);

					TextView tvPrg = (TextView)MyView.findViewById(R.id.grd_item_txtProgress);
					tvPrg.setText("($"+tt.Total+" ~ "+porcentaje + "%)");

					ProgressBar prg = (ProgressBar) MyView.findViewById(R.id.grd_item_prgPorcentaje);
					prg.setMax(total);
					prg.setProgress(tt.Total);

					return MyView;
				}
			};	
			grdPorcentajes.setAdapter(gridAdapter);			
		}
	}	
}
